package com.shm.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.ProvinceCityRegion;
import com.ruoyi.common.core.domain.vo.AreaItemVo;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shm.mapper.ProvinceCityRegionMapper;
import com.shm.service.IProvinceCityRegionService;

/**
 * 省-市-区Service业务层处理
 *
 * @author dam
 * @date 2023-07-15
 */
@Service
public class ProvinceCityRegionServiceImpl implements IProvinceCityRegionService {
    @Autowired
    private ProvinceCityRegionMapper provinceCityRegionMapper;

    @Override
    public List<AreaItemVo> getAreaTree() {
        ////声明变量
        List<AreaItemVo> areaItemVoList = new ArrayList<>();

        //查询出所有省市区数据
        List<ProvinceCityRegion> provinceCityRegionEntityList = provinceCityRegionMapper.selectProvinceCityRegionList(null);
        //过滤出所有省
        List<ProvinceCityRegion> fatherList = provinceCityRegionEntityList.stream().filter(item -> {
            if (item.getParentId() == 0) {
                return true;
            } else {
                return false;
            }
        }).collect(Collectors.toList());
        for (ProvinceCityRegion father : fatherList) {
            AreaItemVo areaItemVo = new AreaItemVo(father.getId(), father.getName());
            this.searchSon(areaItemVo, provinceCityRegionEntityList);
            areaItemVoList.add(areaItemVo);
        }

        return areaItemVoList;
    }

    private void searchSon(AreaItemVo father, List<ProvinceCityRegion> provinceCityRegionEntityList) {
        List<AreaItemVo> sonList = provinceCityRegionEntityList.stream().filter(item -> {
            if (item.getParentId() == father.getValue().intValue()) {
                return true;
            } else {
                return false;
            }
        }).map(item1 -> {
            AreaItemVo son = new AreaItemVo(item1.getId(), item1.getName());
            //继续给儿子寻找孙子
            this.searchSon(son, provinceCityRegionEntityList);
            return son;
        }).collect(Collectors.toList());
        father.setChildren(sonList);
    }

    /**
     * 查询省-市-区
     *
     * @param id 省-市-区主键
     * @return 省-市-区
     */
    @Override
    public ProvinceCityRegion selectProvinceCityRegionById(Long id) {
        return provinceCityRegionMapper.selectProvinceCityRegionById(id);
    }

    @Override
    public List<ProvinceCityRegion> selectProvinceCityRegionByIds(Long[] ids) {
        return provinceCityRegionMapper.selectProvinceCityRegionByIds(ids);
    }

    /**
     * 查询省-市-区列表
     *
     * @param provinceCityRegion 省-市-区
     * @return 省-市-区
     */
    @Override
    public List<ProvinceCityRegion> selectProvinceCityRegionList(ProvinceCityRegion provinceCityRegion) {
        return provinceCityRegionMapper.selectProvinceCityRegionList(provinceCityRegion);
    }

    /**
     * 新增省-市-区
     *
     * @param provinceCityRegion 省-市-区
     * @return 结果
     */
    @Override
    public int insertProvinceCityRegion(ProvinceCityRegion provinceCityRegion) {
        provinceCityRegion.setCreateTime(DateUtils.getNowDate());
        return provinceCityRegionMapper.insertProvinceCityRegion(provinceCityRegion);
    }

    /**
     * 修改省-市-区
     *
     * @param provinceCityRegion 省-市-区
     * @return 结果
     */
    @Override
    public int updateProvinceCityRegion(ProvinceCityRegion provinceCityRegion) {
        provinceCityRegion.setUpdateTime(DateUtils.getNowDate());
        return provinceCityRegionMapper.updateProvinceCityRegion(provinceCityRegion);
    }

    /**
     * 批量删除省-市-区
     *
     * @param ids 需要删除的省-市-区主键
     * @return 结果
     */
    @Override
    public int deleteProvinceCityRegionByIds(Long[] ids) {
        return provinceCityRegionMapper.deleteProvinceCityRegionByIds(ids);
    }

    /**
     * 删除省-市-区信息
     *
     * @param id 省-市-区主键
     * @return 结果
     */
    @Override
    public int deleteProvinceCityRegionById(Long id) {
        return provinceCityRegionMapper.deleteProvinceCityRegionById(id);
    }

    @Override
    public List<ProvinceCityRegion> listRegionByProvinceId(Long provinceId) {
        return provinceCityRegionMapper.listRegionByProvinceId(provinceId);
    }
}
